# -*-coding:utf-8-*-

from selenium import webdriver
from time import sleep
from selenium.webdriver.support.ui import Select
import random
import sys

sys.path.append('..\cite')
from cite import shortcuts
import os  # 执行一个文件

dr = webdriver.Firefox()  # 开启Firefox浏览器
dr.implicitly_wait(5)  # 等待秒
dr.maximize_window()  # 控制窗口
first_url = shortcuts.CRMurl
dr.get(first_url)
sleep(4)

dr.find_element_by_xpath('/html/body/div[2]/div/div[2]/div/form/fieldset/input[1]').clear()
dr.find_element_by_xpath('/html/body/div[2]/div/div[2]/div/form/fieldset/input[1]').send_keys(shortcuts.CRMnam)
sleep(2)

# name1 = u"贸易门店"
name1 = u"贸易工厂"

dr.find_element_by_xpath('/html/body/div[2]/div/div[2]/div/form/fieldset/input[2]').clear()
dr.find_element_by_xpath('/html/body/div[2]/div/div[2]/div/form/fieldset/input[2]').send_keys(
    shortcuts.CRMpassword)  # 账号密码
sleep(2)

dr.find_element_by_xpath('/html/body/div[2]/div/div[2]/div/form/fieldset/input[3]').click()
sleep(3)

dr.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/ul[1]/li[1]/a').click()
sleep(2)

dr.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/ul[1]/li[1]/a').click()  # 点击产品按钮
sleep(4)

# dr.find_element_by_xpath('/html/body/div[3]/div[2]/div[1]/ul[2]/li[1]/a').click()  # 点击零售产品按钮
# sleep(3)


x1 = 0  # 商品类目排序
x2 = 0
x3 = 0
y = 0

to1 = 0  # 商品分类的滚动条滑动距离
to2 = 0
to3 = 0

rmb = 10
# 循环选择商品类别
for x in range(50):
    try:

        if y == 3:  # 如果程序连续跳出两次以上，则说明二级类目已经走完，则一级类目加1
            x1 = x1 + 1
            to1 = to1 + 23

            x2 = 0
            to2 = 0
            to3 = 0

        dr.find_element_by_xpath('/html/body/div[3]/div[2]/div[2]/div[2]/a').click()
        sleep(3)

        sle = random.choice('abcdefghlyzkiu')
        se = random.choice([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
        sc = random.choice([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
        A1 = sle + bytes(se) + bytes(sc)
        name = u"批发 2"
        name_XX = name + A1

        dr.find_element_by_xpath('//*[@id="name"]').clear()
        dr.find_element_by_xpath('//*[@id="name"]').send_keys(name_XX)  # 从shortcuts中拿出商品名
        # dr.find_element_by_xpath('//*[@id="name"]').send_keys((u'%s %s %s%s%s 名称超级超级超级长哦' ) % (name1,a1,x1,x2,x3))
        sleep(2)

        dr.find_element_by_xpath('//*[@id="crm_gnhpxk"]').clear()
        dr.find_element_by_xpath('//*[@id="crm_gnhpxk"]').send_keys(u'vvvv型号')
        sleep(2)

        dr.find_element_by_xpath('//*[@id="crm_efiily"]').clear()
        dr.find_element_by_xpath('//*[@id="crm_efiily"]').send_keys(u'ccc品牌')
        sleep(2)

        dr.find_element_by_xpath('//*[@id="#"]').click()  # 点击产品分类选择按钮
        sleep(4)

        js1 = 'document.getElementById("typelist1").scrollTop=%r' % to1  # 滑动类目的滚动条
        dr.execute_script(js1)
        sleep(1)

        i = dr.find_element_by_xpath('//*[@id="typelist1"]')  # 首先定位到最外面的框架
        elements = i.find_elements_by_class_name("typeli")
        elements[x1].click()  # 定位输入框写入内
        sleep(1)

        js2 = 'document.getElementById("typelist2").scrollTop=%r' % to2
        dr.execute_script(js2)
        sleep(1)

        i = dr.find_element_by_xpath('//*[@id="typelist2"]')
        elements = i.find_elements_by_class_name("typeli")
        elements[x2].click()
        sleep(1)

        js3 = 'document.getElementById("typelist3").scrollTop=%r' % to3
        dr.execute_script(js3)
        sleep(1)

        i = dr.find_element_by_xpath('//*[@id="typelist3"]')
        elements = i.find_elements_by_class_name("typeli")
        elements[x3].click()
        sleep(1)

        up = dr.find_element_by_xpath('//*[@id="editor"]/div[2]/div[1]')
        up.send_keys(u"亲爱滴小妹妹不要不要哭泣，你就是一个一个一个一个大坏蛋")
        sleep(2)

        js = "var q=document.documentElement.scrollTop=1000"  # 向下拖拽浏览器的滚动条
        dr.execute_script(js)
        sleep(3)

        img = random.choice(
            [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 29, 20, 21, 22, 23, 24, 25])

        # 获取项目根目录的相对路径后在拼写图片位置路径
        file_path = os.path.dirname(os.path.abspath('.')) + '/picture'

        up = dr.find_element_by_xpath('//*[@id="main_pic"]')
        up.send_keys("%r\img%r.jpg" % (file_path, img))
        sleep(4)
        r = dr.switch_to_alert()
        sleep(2)
        print (r.text)
        r.accept()

        dr.find_element_by_xpath('//*[@id="btn_add_files"]').click()
        sleep(2)

        dr.find_element_by_xpath('//*[@id="btn_add_files"]').click()
        sleep(2)

        img = random.choice(
            [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 29, 20, 21, 22, 23, 24, 25])

        up = dr.find_element_by_xpath('//*[@id="sec_pic_1"]')
        up.send_keys("%r\img%r.jpg" % (file_path, img))
        sleep(4)
        r = dr.switch_to_alert()
        sleep(2)
        print (r.text)
        r.accept()

        img = random.choice(
            [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 29, 20, 21, 22, 23, 24, 25])

        up = dr.find_element_by_xpath('//*[@id="sec_pic_2"]')
        up.send_keys("%r\img%r.jpg" % (file_path, img))
        sleep(4)
        r = dr.switch_to_alert()
        sleep(2)
        print (r.text)
        r.accept()

        Select(dr.find_element_by_xpath('//*[@id="crm_fwuzfa"]')).select_by_value("个")
        sleep(2)

        dr.find_element_by_xpath('//*[@id="price"]/tbody/tr[2]/td[1]/input').clear()
        dr.find_element_by_xpath('//*[@id="price"]/tbody/tr[2]/td[1]/input').send_keys('10')
        sleep(2)

        dr.find_element_by_xpath('//*[@id="price"]/tbody/tr[2]/td[2]/input').clear()
        dr.find_element_by_xpath('//*[@id="price"]/tbody/tr[2]/td[2]/input').send_keys('10')
        sleep(2)

        """
		dr.find_element_by_class_name('unitprice').clear()
		dr.find_element_by_class_name('unitprice').send_keys('5')
		"""

        dr.find_element_by_class_name('originalPrice').clear()
        dr.find_element_by_class_name('originalPrice').send_keys('10')
        sleep(2)

        dr.find_element_by_xpath('//*[@id="total"]').clear()
        dr.find_element_by_xpath('//*[@id="total"]').send_keys('%r' % rmb)

        js = "var q=document.documentElement.scrollTop=1000"  # 向下拖拽浏览器的滚动条
        dr.execute_script(js)
        sleep(3)

        Select(dr.find_element_by_xpath('//*[@id="province"]')).select_by_value("北京市")
        sleep(2)

        Select(dr.find_element_by_xpath('//*[@id="city"]')).select_by_value("市辖区")
        sleep(2)

        Select(dr.find_element_by_xpath('//*[@id="town"]')).select_by_value("东城区")
        sleep(2)

        dr.find_element_by_xpath('//*[@id="address"]').clear()
        dr.find_element_by_xpath('//*[@id="address"]').send_keys(u'第卅街道')
        sleep(2)

        dr.find_element_by_xpath('//*[@id="goodsweight"]').clear()
        dr.find_element_by_xpath('//*[@id="goodsweight"]').send_keys(1)
        sleep(3)

        dr.find_element_by_xpath('//*[@id="submit"]').click()
        sleep(4)

        r = dr.switch_to_alert()
        sleep(3)
        print (r.text)
        r.accept()

        x3 = x3 + 1  # 没执行一次则三级商品类目自动加1
        print ('x3= %r' % x3)
        y = 0
        to3 = to3 + 18
        rmb = rmb + 10
        sleep(3)

        print u'发布成功'

    except:
        x2 = x2 + 1
        to2 = to2 + 20
        x3 = 0
        to3 = 0
        print ('x2= %r' % x2)
        print ('x3= %r' % x3)
        dr.back()

        y = y + 1  # 用来获取程序跳出次数
        print ('y= %r' % y)
        sleep(3)
        print u'发布失败'

sleep(2)
# dr.close()


# os.system('python ../pc/pc_order.py')   # 执行下一个文件
